/*
 * @Author: 吴世扬 18368095041@163.com
 * @Date: 2024-10-16 08:29:49
 * @LastEditors: 吴世扬 18368095041@163.com
 * @LastEditTime: 2024-10-16 08:36:48
 * @FilePath: /react18-vite-admin/src/views/system/RolesManage/useTable.tsx
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import { useEffect } from 'react';
import { immerable } from 'immer';
import { useImmer } from 'use-immer';
import type { TableProps } from 'antd';

import { useRoleList, RoleListParam } from '@/service/biz/role/useRoleList';
import { usePaginationHelper } from '@/service/app/usePaginationHelper';
import type { RoleListItem } from '@/typings/api/role';
import Button from '@/components/ui/Button/Button';

class ListParam extends RoleListParam {
    [immerable] = true;
    timeStamp = Date.now();
}

export const useTable = () => {

    const { total, list, loading, getList: getUserList } = useRoleList();

    const [listParam, setListParam] = useImmer(new ListParam());

    const [editNsp, setEditNsp] = useImmer({
        open: false,
        id: undefined as undefined | number
    });

    const paginationConfig = usePaginationHelper(
        total,
        listParam.size,
        listParam.current,
        (page, pageSize) => {
            setListParam((draft) => {
                draft.current = page;
                draft.size = pageSize;
            });
            getList();
        }
    );

    const getList = () => {
        setListParam((draft) => {
            draft.timeStamp = Date.now();
        });
    };

    const reset = () => {
        setListParam(new ListParam());
        getList();
    };

    const handleEdit = (id?: number) => {
        setEditNsp((draft) => {
            draft.id = id;
            draft.open = true;
        });
    };

    const afterUpdate = () => {
        window.location.reload();
    }

    useEffect(() => {
        getUserList(listParam);
    }, [listParam.timeStamp]);

    const columns: TableProps<RoleListItem>['columns'] = [
        {
            title: 'ID',
            dataIndex: 'id'
        },
        {
            title: '角色名称',
            dataIndex: 'name'
        },
        {
            title: '角色描述',
            dataIndex: 'description'
        },
        {
            title: '创建时间',
            dataIndex: 'createTime'
        },
        {
            title: '操作',
            render: (_, record) => (
                <div className="flex-row jc-center">
                    <Button
                        type="primary"
                        onClick={() => handleEdit(record.id)}
                    >
                        编辑
                    </Button>
                </div>
            )
        }
    ];
    return {
        columns,
        editNsp,
        setEditNsp,
        loading,
        list,
        total,
        listParam,
        setListParam,
        getList,
        reset,
        handleEdit,
        paginationConfig,
        afterUpdate
    };
};
